﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace BookInfoExtractor
{
    public partial class Form2 : Form
    {
        BookPattern objPattern = null;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            objPattern = Form1.objPattern;
            string fileName = "Patterns.xml";
            XmlDocument xmlDoc = new XmlDocument();
            if (!File.Exists(fileName))
            {
                xmlDoc.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Patterns", ""));
                xmlDoc.Save(fileName);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string fileName = "Patterns.xml";
            XmlDocument xmlDoc = new XmlDocument();

            if(!File.Exists(fileName))
            {
                xmlDoc.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Patterns", ""));     
                xmlDoc.Save(fileName);  
            }
            else
            {
                xmlDoc.Load(fileName);

                dynamic domElement = objPattern.Element.DomElement;
                string elemClass = domElement.className;
                XmlNode rootNode = xmlDoc.DocumentElement;

                XmlNode patternNode = xmlDoc.CreateNode(XmlNodeType.Element, "Pattern", "");
     
                XmlNode patternNameNode = xmlDoc.CreateNode(XmlNodeType.Element, "PatternName", "");
                patternNameNode.InnerText = txtPatternName.Text;
                
                XmlNode mainElementNode = xmlDoc.CreateNode(XmlNodeType.Element, "BaseElement", "");
                mainElementNode.InnerText = objPattern.Element.TagName;

                XmlNode mainElementClassNameNode = xmlDoc.CreateNode(XmlNodeType.Element, "BaseElementClassName", "");
                mainElementClassNameNode.InnerText = elemClass;

                XmlNode similarClassNameNode = xmlDoc.CreateNode(XmlNodeType.Element, "ClassNameToSearch", "");
                similarClassNameNode.InnerText = objPattern.ClassNameToSearch;

                XmlNode mainElementIDNode = xmlDoc.CreateNode(XmlNodeType.Element, "BaseElementID", "");
                mainElementIDNode.InnerText = objPattern.Element.Id;

                XmlNode patternURLNode = xmlDoc.CreateNode(XmlNodeType.Element, "PatternURL", "");
                patternURLNode.InnerText = objPattern.Element.Document.Url.ToString();

                string authorPos = string.Empty;
                foreach (int pos in objPattern.GetAuthorPosition())
                {
                    authorPos = authorPos + pos.ToString() + ", "; 
                }
                authorPos = authorPos.Trim(new char[] { ',' });

                XmlNode authorPositionNode = xmlDoc.CreateNode(XmlNodeType.Element, "AuthorPosition", "");
                authorPositionNode.InnerText = authorPos;

                string titlePos = string.Empty;
                foreach (int pos in objPattern.GetTitlePosition())
                {
                    titlePos = titlePos + pos.ToString() + ", ";
                }
                titlePos = titlePos.Trim(new char[] { ',' });

                XmlNode titlePosNode = xmlDoc.CreateNode(XmlNodeType.Element, "TitlePosition", "");
                titlePosNode.InnerText = titlePos;

                string imagePos = string.Empty;
                foreach (int pos in objPattern.GetImagePosition())
                {
                    imagePos = imagePos + pos.ToString() + ", ";
                }
                imagePos = imagePos.Trim(new char[] { ',' });

                XmlNode imagePosNode = xmlDoc.CreateNode(XmlNodeType.Element, "ImagePosition", "");
                imagePosNode.InnerText = imagePos;

                string isbnPos = string.Empty;
                foreach (int pos in objPattern.GetISBNPosition())
                {
                    isbnPos = isbnPos + pos.ToString() + ", ";
                }
                isbnPos = isbnPos.Trim(new char[] { ',' });

                XmlNode isbnPosNode = xmlDoc.CreateNode(XmlNodeType.Element, "ISBNPosition", "");
                isbnPosNode.InnerText = isbnPos;

                string pricePos = string.Empty;
                foreach (int pos in objPattern.GetPricePosition())
                {
                    pricePos = pricePos + pos.ToString() + ", ";
                }
                pricePos = pricePos.Trim(new char[] { ',' });

                XmlNode pricePosNode = xmlDoc.CreateNode(XmlNodeType.Element, "PricePosition", "");
                pricePosNode.InnerText = pricePos;

                patternNode.AppendChild(patternNameNode);
                patternNode.AppendChild(patternURLNode);   
                patternNode.AppendChild(mainElementNode);
                patternNode.AppendChild(mainElementClassNameNode);
                patternNode.AppendChild(similarClassNameNode);
                patternNode.AppendChild(mainElementIDNode);
                patternNode.AppendChild(authorPositionNode);
                patternNode.AppendChild(titlePosNode);
                patternNode.AppendChild(imagePosNode);
                patternNode.AppendChild(isbnPosNode);
                patternNode.AppendChild(pricePosNode);

                rootNode.AppendChild(patternNode);

                xmlDoc.Save(fileName);   
            }

            this.Close(); 
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }
    }
}
